package com.bdqn.mzc.model;

import lombok.Data;

import java.text.MessageFormat;

@Data
public class Result<T> {
        //数据
        private T data;
        //状态
        private boolean success;
        //状态码
        private Integer code;
        //信息
        private String message;

        public Result() {
        }

        //接口正常返回执行
        public static <T> Result<T> wrapSuccessfulResult(T data) {
            Result<T> result = new Result<T>();
            result.data = data;
            result.success = true;
            result.code = 200;
            return result;
        }

        public static <T> Result<T> wrapSuccessfulResult(String message, T data) {
            Result<T> result = new Result<T>();
            result.data = data;
            result.success = true;
            result.code = 200;
            result.message = message;
            return result;
        }

        //接口异常时返回
        public static <T> Result<T> wrapErrorResult(ErrorCodes error) {
            Result<T> result = new Result<T>();
            result.success = false;
            result.code = error.getCode();
            result.message = error.getMessage();
            return result;
        }

        public static <T> Result<T> wrapErrorResult(ErrorCodes error, Object... extendMsg) {
            Result<T> result = new Result<T>();
            result.success = false;
            result.code = error.getCode();
            // MessageFormat  能把字符里的占位符{0} 替换为指定字符串。
            result.message = MessageFormat.format(error.getMessage(), extendMsg);
            return result;
        }

        public static <T> Result<T> wrapErrorResult(Integer code, String message) {
            Result<T> result = new Result<T>();
            result.success = false;
            result.code = code;
            result.message = message;
            return result;
        }

}
